package cn.tedu.smartos.admin.service.impl;

import cn.tedu.smartos.admin.mapper.PointsGoodsMapper;
import cn.tedu.smartos.admin.pojo.dto.PointsGoodsAddParam;
import cn.tedu.smartos.admin.pojo.dto.PointsGoodsUpdateParam;
import cn.tedu.smartos.admin.pojo.entity.PointsGoods;
import cn.tedu.smartos.admin.pojo.vo.PointsGoodsVO;
import cn.tedu.smartos.admin.service.PointsGoodsService;
import cn.tedu.smartos.base.exception.ServiceException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.core.util.CollectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class PointsGoodsServiceImpl implements PointsGoodsService {
    @Autowired
    private PointsGoodsMapper pointsGoodsMapper;

    @Override
    public void add(PointsGoodsAddParam addParam) {
        log.debug("开始处理【添加积分商品】的业务，参数：{}", addParam);
        PointsGoods pointsGoods = new PointsGoods();
        BeanUtils.copyProperties(addParam, pointsGoods);
        pointsGoods.setCreateTime(new Date());
        pointsGoods.setUpdateTime(new Date());

        int rows = pointsGoodsMapper.insert(pointsGoods);
        if (rows != 1) {
            throw new ServiceException("添加积分商品失败");
        }
    }

    @Override
    public void update(PointsGoodsUpdateParam updateParam) {
        log.debug("开始处理【修改积分商品】的业务，参数：{}", updateParam);
        PointsGoods pointsGoods = new PointsGoods();
        BeanUtils.copyProperties(updateParam, pointsGoods);
        pointsGoods.setUpdateTime(new Date());

        int rows = pointsGoodsMapper.update(pointsGoods);
        if (rows != 1) {
            throw new ServiceException("修改积分商品失败");
        }
    }

    @Override
    public void delete(List<Long> goodsIds) {
        log.debug("开始处理【删除积分商品】的业务，参数：{}", goodsIds);
        if (CollectionUtils.isEmpty(goodsIds)) {
            throw new IllegalArgumentException("商品ID列表不能为空");
        }

        int rows = pointsGoodsMapper.deleteByIdInBatch(goodsIds);
        if (rows != goodsIds.size()) {
            throw new ServiceException("部分商品删除失败");
        }
    }

    @Override
    public PageInfo<PointsGoodsVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询积分商品列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<PointsGoodsVO> list = pointsGoodsMapper.list();
        return new PageInfo<>(list);
    }

    @Override
    public PointsGoodsVO detail(Long goodsId) {
        log.debug("开始处理【查询积分商品详情】的业务，参数：{}", goodsId);
        return pointsGoodsMapper.detail(goodsId);
    }

    @Override
    public void toggleStatus(Long goodsId) {
        log.debug("开始处理【切换积分商品状态】的业务，参数：{}", goodsId);
        PointsGoodsVO goods = pointsGoodsMapper.detail(goodsId);
        if (goods == null) {
            throw new ServiceException("商品不存在");
        }

        int newStatus = goods.getIsActive() == 1 ? 0 : 1;
        int rows = pointsGoodsMapper.updateStatus(goodsId, newStatus);
        if (rows != 1) {
            throw new ServiceException("切换商品状态失败");
        }
    }
}